package com.leetcode.leetcode2;

/**
 *  合并两个有序链表
 * @author: luhd
 * @create: 2021-09-01
 */
public class Practice21 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);

    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);    // 链表头节点
        ListNode tail = head;   // 链表尾节点
        while(l1!=null && l2!=null){
            if(l1.val<l2.val){
                // l1表示的是头节点
                tail.next = l1;
                l1 = l1.next;
                tail = tail.next;
            }else {
                tail.next = l2;
                l2 = l2.next;
                tail = tail.next;
            }
        }
        while(l1!=null){
            tail.next = l1;
            l1 = l1.next;
            tail = tail.next;
        }
        while(l2!=null){
            tail.next = l2;
            l2 = l2.next;
            tail = tail.next;
        }
        return head.next;
    }
}
